import { inject, injectable } from "inversify";
import { Terminal, Uri } from "vscode";
import { IActiveResourceService, ITerminalManager } from "../common/application/types";
import { IDisposable, IDisposableRegistry } from "../common/types";
import { ITerminalActivator, ITerminalAutoActivation } from "./types";

@injectable()
export class TerminalAutoActivation implements ITerminalAutoActivation {
  private handler?: IDisposable;

  private readonly terminalsNotToAutoActivate = new WeakSet<Terminal>();

  constructor(
    @inject(ITerminalManager)
    private readonly terminalManager: ITerminalManager,
    @inject(IDisposableRegistry) disposableRegistry: IDisposableRegistry,
    @inject(ITerminalActivator) private readonly activator: ITerminalActivator,
    @inject(IActiveResourceService)
    private readonly activeResourceService: IActiveResourceService,
  ) {
    disposableRegistry.push(this);
  }
  public register(): void {
    if (this.handler) {
      return;
    }
    this.handler = this.terminalManager.onDidOpenTerminal(this.activateTerminal, this);
  }
  private async activateTerminal(terminal: Terminal): Promise<void> {
    if (this.terminalsNotToAutoActivate.has(terminal)) {
        return;
    }
    if ('hideFromUser' in terminal.creationOptions && terminal.creationOptions.hideFromUser) {
        return;
    }

    const cwd =
        'cwd' in terminal.creationOptions
            ? terminal.creationOptions.cwd
            : this.activeResourceService.getActiveResource();
    const resource = typeof cwd === 'string' ? Uri.file(cwd) : cwd;

    await this.activator.activateEnvironmentInTerminal(terminal, {
        resource,
    });
}
  disableAutoActivation(terminal: Terminal): void {
    throw new Error("76Method not implemented.");
  }
  dispose(): void | undefined {
    throw new Error("77Method not implemented.");
  }
}